/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.ui.edge;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import net.worlds.Util;
import net.worlds.ui.UI;

/**
 *
 * @author Jacob Morgan
 */
public class ELabel extends EComponent{
    private EActionListener actionListener;
    private String text;
    private Font font = EDGESettings.defaultFont;
    private final Color disabledColor = new Color(160, 160, 160);
    private Color textColor = new Color(255, 255, 255);
    private Color shadowColor = new Color(0, 0, 0);
    private Color backgroundColor = null;
    private Color hoverColor = null;
    private int textHeight;
    private int textWidth;
    private int textOffset;
    public ELabel(String text)
    {
        setText(text);
    }
    public void setHoverColor(Color c)
    {
        hoverColor = c;
    }
    public void setActionListener(EActionListener al)
    {
        actionListener = al;
    }
    public void setActionListener(int k, EActionListener al)
    {
        setActionListener(al);
        UI.addKeyboardShortcut(k, al);
    }
    public void setText(String t)
    {
        text = t;
        Rectangle2D bounds = font.getStringBounds(t, Util.fontRendererContext);
        textWidth = (int) bounds.getWidth();
        textHeight = (int) bounds.getHeight();
        height = textHeight;
        width = textWidth;
    }
    public void setBackgroundColor(Color c)
    {
        backgroundColor = c;
    }
    @Override
    public void mouseOver()
    {
        super.mouseOver();
        if(hoverColor != null)
            backgroundColor = hoverColor;
    }
    @Override
    public void mouseOff()
    {
        if(hoverColor != null)
            backgroundColor = null;
    }
    @Override
    public void mousePressed()
    {
        if(actionListener != null)
            actionListener.actionPerformed();
    }
    public void setTextOffset(int x)
    {
        textOffset = x;
    }
    public void centerText()
    {
        textOffset = width/2 - textWidth/2;
    }
    public int getTextOffset()
    {
        return textOffset;
    }
    public void setColor(Color c)
    {
        textColor = c;
    }
    public String getText()
    {
        return text;
    }
    public void setFont(Font f)
    {
        font = f;
    }
    @Override
    public void render(Graphics2D g)
    {
        super.render(g);
        //if(shadowColor != null)
        //    drawText(g, font, text, shadowColor, getX() + 1, getY() + height + 1);
        int diff = height - textHeight;
        g.setColor(backgroundColor);
        if(backgroundColor != null)
            g.fillRect(getX(), getY(), width, height);
        Color c;
        if(enabled)
            c = textColor;
        else
            c = disabledColor;
        drawText(g, font, text, c, getX() + textOffset, getY() + diff/2 + textHeight);
    }
}
